
#!/uer/bin/perl


%ages = ('Martin' => 28,
         'Sharon' => 35,
         'Rikke' => 29,);

print "Rikke is $ages{Rikke} years old\n";

#another way
$ages{Martin} = 28;

%hash = ('Fred' , 'Flintstone', 'Barney', 'Rubble');

#Extracting Individual Elements
print $hash{Fred};

#Extracting Slices

%hash = (-Fred => 'Flintstone', -Barney => 'Rubble');
print join("\n",@hash{-Fred,-Barney});


#Extracting Keys and Values
%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
print "The following are in the DB: ",join(', ',values %ages),"\n";


%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
foreach $key (%ages)
{
  print "$key is $ages{$key} years  old\n";
}



#Checking for Existence

%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
if (exists($ages{"mohammad"}))
{
  print "mohammad if $ages{$name} years old\n";
}
else
{
  print "I don't know the age of mohammad\n";
}


#Sorting/Ordering Hashes

print(join(', ',keys %hash),"\n");
print(join(', ',keys %hash),"\n");

#Hash Size
%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
print "Hash size: ",scalar keys %ages,"\n";


#Add & Remove Elements in Hashes
%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);

# Add one more element in the hash
$age{'John'} = 40;
# Remove one element from the hash
delete( $age{'Sharon'} );